/*
 * Copyright (C) 2015-2016  Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


#include "PlanetWars2dRT-core/specific/controller/game_end_conditions.h"
#include "PlanetWars2dRT-core/specific/model/universe_planets.h"


EXTERN_C_BEGIN


unsigned char
game_get_winner_index_no_enemy_planet(const struct game_t* game)
{
  if(game == NULL)
    {
      return NULL_GAME_WINNER_INDEX;
    }
  
  unsigned char result = NULL_GAME_WINNER_INDEX;
  const size_t nb_planets = universe_get_nb_planets(&game->universe);
  for(size_t i = 0; i < nb_planets; ++i)
    {
      unsigned char tmp;
      tmp = universe_get_const_planet_no_check(&game->universe, i)->player_index;
      
      if(tmp != result && tmp != NULL_GAME_WINNER_INDEX)
	{
	  if(result == NULL_GAME_WINNER_INDEX)
	    {
	      result = tmp;
	    }
	  else
	    {
	      return NULL_GAME_WINNER_INDEX;
	    }
	}
    }
  return result;
}


EXTERN_C_END
